package gateway

import (
	"time"
	"sync"
)

var rwmutex sync.RWMutex
var sessions map[string]time.Time

func init()  {
	sessions = make(map[string]time.Time)

	go func() {
		for {
			select {
			case <-time.After(1*time.Minute):
				rwmutex.Lock()
				for sid,t:= range sessions  {
					off := time.Now().Sub(t)
					if off.Hours() >= 1 {
						delete(sessions,sid)
					}
				}
				rwmutex.Unlock()
				break
			}
		}
	}()
}

func setSession(sid string)  {
	rwmutex.Lock()
	sessions[sid] = time.Now()
	rwmutex.Unlock()
}

func checkSession(sid string) bool{
	rwmutex.RLock()
	_,ok := sessions[sid]
	rwmutex.RUnlock()
	return ok
}
